//
//  DetailViewController.m
//  StoryboardSplitAndPopover
//
//  Created by ficow on 31/7/16.
//  Copyright © 2016年 ficow. All rights reserved.
//

#import "DetailViewController.h"
#import "InfoViewController.h"

@interface DetailViewController ()<UIPopoverControllerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *btn;
@property (weak, nonatomic) IBOutlet UIToolbar *barBtn;
@property (nonatomic, strong) UIPopoverController *popVC;

@end

@implementation DetailViewController


#pragma mark - Managing the detail item
- (IBAction)showInfoVC:(id)sender {
    
}

- (void)setDetailItem:(id)newDetailItem {
    if (_detailItem != newDetailItem) {
        _detailItem = newDetailItem;
            
        // Update the view.
        [self configureView];
    }
}

- (void)configureView {
    // Update the user interface for the detail item.
    if (self.detailItem) {
        self.detailDescriptionLabel.text = [self.detailItem description];
    }
    InfoViewController *ivc = [self.storyboard instantiateViewControllerWithIdentifier:@"InfoViewController"];
    _popVC = [[UIPopoverController alloc]initWithContentViewController:ivc];
    _popVC.contentViewController.view.backgroundColor = [UIColor greenColor];
    _popVC.popoverContentSize = CGSizeMake(320., 400.);
    _popVC.delegate = self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self configureView];
}

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController{
    NSLog(@"popover dismissed.");
}

//InfoVCBarSegue
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([[segue identifier] isEqualToString:@"InfoVCSegue"]){
        InfoViewController *InfoVC = segue.destinationViewController;
        InfoVC.label.text = @"nothing";
//        if (_popVC.popoverVisible == YES) {
//            [_popVC dismissPopoverAnimated:YES];
//            return;
//        }
//        [_popVC presentPopoverFromRect:self.btn.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    }
    else if([[segue identifier] isEqualToString:@"InfoVCBarSegue"]){
        
//        if (_popVC.popoverVisible == YES) {
//            [_popVC dismissPopoverAnimated:YES];
//            return;
//        }
//        
//        [_popVC presentPopoverFromBarButtonItem:self.barBtn.items.firstObject permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
    }
}

@end
